<?php

$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 2;
$keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
$start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
$limit = 10;

global $wpdb;
if($type == 1){//已录入
    $sql = "select spider_fpv.*,wp_posts.post_title from spider_fpv left join wp_posts on spider_fpv.wp_posts_id = wp_posts.ID where wp_posts_id>0 and  post_title like '%{$keyword}%' order by create_time DESC";
    $fpvs = $wpdb->get_results($sql." limit ".$start*$limit.",".$limit);

    $total = count($wpdb->get_results($sql));

}elseif ($type == 2){//未录入
    $sql = "select * from spider_fpv where wp_posts_id=0 order by create_time DESC";
    $fpvs = $wpdb->get_results($sql." limit ".$start*$limit.",".$limit);
    $total = count($wpdb->get_results($sql));

}

?>
<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
        table{
            border-collapse:collapse;
        }
        table tr th{
            text-align: center;
        }
        table tr th,td{
            border: 1px solid #000;
            padding: 5px;
        }
        .navigation ul{
            display: inline-block;
        }
        .navigation ul li{
            display: block;
            float: left;
            width: 20px;
            height: 20px;
            background-color: grey;
            margin-right: 5px;
        }
        .navigation ul li a{
            color: white;
            text-align: center;
            display: block;
            width: 100%;
            height: 100%;
            text-decoration: none;
        }
        .navigation ul li.active{
            background-color: white;
        }
        .navigation ul li.active a{
            color: black;
            text-decoration: underline;
        }


    </style>
</head>
<body>
<div class="wrap">
    <form name="search" method="POST" action="" enctype="" >
        <select name="type">
            <option value="1" <?php if($type==1){echo 'selected';} ?>>已录入</option>
            <option value="2" <?php if($type==2){echo 'selected';} ?>>未录入</option>
        </select>
        <input type="text" name="keyword" value="">
        <input type="submit" name="submit" value="查询">总共<?php echo $total; ?>个记录
    </form>

    <table>
        <tr>
            <th>ID</th>
            <th>链接</th>
            <th>日期</th>
            <th>图片数量</th>
            <th>配件数量</th>
            <?php if($type == 1){ ?>
                <th>标题</th>
            <?php } ?>
            <th>操作</th>
        </tr>
        <?php foreach ($fpvs as $fpv) { ?>
        <tr>
            <td><?php echo $fpv->id; ?></td>
            <td><?php echo $fpv->url; ?></td>
            <td><?php echo $fpv->create_time; ?></td>
            <td><?php echo empty($fpv->photos) ? 0 : count(explode(',',$fpv->photos)) ?></td>
            <td><?php echo empty($fpv->parts) ? 0 : count(explode('@@@',$fpv->parts)) ?></td>
            <?php if($type == 1){ ?>
                <td><?php echo $fpv->post_title; ?></td>
            <?php } ?>
            <td><a href="/wp-admin/admin.php?page=edit-part&id=<?php echo $fpv->id; ?>">编辑</a></td>
        </tr>
        <?php } ?>
    </table>

    <div class="navigation">
        <ul>
            <?php for ($i=0;$i<ceil($total/$limit);$i++) {?>
                <li <?php if ($start==$i){?>class="active"<?php } ?>><a href="<?php echo $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; ?>&type=<?php echo $type; ?>&keyword=<?php echo $keyword; ?>&start=<?php echo $i; ?>"><?php echo $i+1; ?></a></li>
            <?php } ?>
        </ul>
    </div>
</div>


<script type="text/javascript">




</script>
</body>
</html>